package com.treasure.mall.zbqmallapi.resolver;

import com.treasure.mall.biz.bo.SystemInfoData;
import com.treasure.mall.zbqmallapi.annotation.SystemInfo;
import lombok.AllArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>
 * 系统信息
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.resolver
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/3 1:53 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@AllArgsConstructor
public class ClientArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.hasParameterAnnotation(SystemInfo.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        SystemInfoData systemInfoData = new SystemInfoData();

        HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        if (httpServletRequest == null) {
            return systemInfoData;
        }
        systemInfoData.setClient(httpServletRequest.getHeader("X-client"));
        systemInfoData.setSystemInfo(httpServletRequest.getHeader("X-systemInfo"));
        systemInfoData.setSystemVersion(httpServletRequest.getHeader("X-systemVersion"));
        systemInfoData.setVersion(httpServletRequest.getHeader("X-version"));
        systemInfoData.setScene(httpServletRequest.getHeader("X-scene"));
        return systemInfoData;
    }
}
